package com.markerhub.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.markerhub.entity.User;
import com.markerhub.mapper.UserMapper;
import com.markerhub.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public boolean register(User user) {
        // 验证用户名是否存在
        if (lambdaQuery().eq(User::getUsername, user.getUsername()).count() > 0) {
            throw new RuntimeException("用户名已存在");
        }

        // 密码加密
        user.setPassword(SecureUtil.md5(user.getPassword()));

        // 设置用户的默认值
        user.setCreated(LocalDateTime.now());
        user.setLastLogin(LocalDateTime.now());
        user.setStatus(1); // 设置默认状态为启用

        // 插入数据库
        return save(user);
    }
    }

